package 网络抢票;

public class BuyTicket implements Runnable{
    private static int Ticket=10;
    private static int num=0;

    private synchronized static void show(){
        Ticket--;
        num++;
        System.out.println(Thread.currentThread().getName()+"抢到第"+num+"张票,剩余"+(10-num)+"张票！");

    }
    @Override
    public void run() {
           while (true){
               show();
               if (Thread.currentThread().getName().equals("黄牛党")){
                   return;
               }
               try {
                   Thread.sleep(500);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               if (Ticket==0){
                   System.out.println("票已经被抢完了");
                   break;
               }
           }
    }

    public static void main(String[] args) {
        new Thread(new BuyTicket(),"桃跑跑").start();
        new Thread(new BuyTicket(),"张飘飘").start();
        new Thread(new BuyTicket(),"黄牛党").start();
    }
}
